package simulation1;

import java.util.Date;
import java.util.Random;


public class Car extends Thread{
	
	int id,lane,segment;
	double speed;
	private Road road;
	double timeTravelled;
	String goLeft; 
	//boolean LaneAvailable;
	
	Car(int id,Road road,int lane,int segment,double speed){
		this.id= id;
		this.road = road;
		this.lane = lane;
		this.segment = segment;
		this.speed = speed;
		start();
	}
	
	public void run(){
		startCar();
	}

	public void startCar() {
		
		System.out.println("Speed of Car["+id+"]="+this.speed);
		timeTravelled = (Road.block/this.speed)*1000;
		System.out.println("Time travelled by Car["+id+"]="+timeTravelled/1000);
		//String s= Double.toString(timeTravelled);
		
		road.request_initialPosition(this,id,lane,segment);
		
		try {
			Thread.sleep((long)(timeTravelled));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		while(segment<=road.no_of_segments-2){
		
		road.request_Availability(this,id,lane,segment);
			
			try {
				Thread.sleep((long)(timeTravelled));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		
		
		
	  }
		
		if(segment==road.no_of_segments-1)
		{
			road.position[lane][segment] = Road.AVAIL;
			System.out.println("CAR "+id+" HAS COMPLETED IT'S RIDE!");
		}
		
		
	}
	
	
	
}
